package com.tz.crm.dao.impl;

import com.tz.crm.dao.ContactDao;
import com.tz.crm.entity.Contact;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 使用 @Repository 将 DAO 类声明为 Bean
 */
@Repository
@Transactional
public class ContactDaoImpl extends BaseDaoImpl<Contact, String> implements ContactDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Contact> getByCustomerId(String id) {
        String hql = "from Contact c where c.customer.id=:id order by c.major desc";
        Query query = getSession().createQuery(hql).setParameter("id", id);
        List<Contact> contractorList = query.list();
        return contractorList;
    }

    @Override
    public Contact getMajorByCustomerId(String id) {
        String hql = "from Contact c where c.customer.id=:id and c.major=true";
        Query query = getSession().createQuery(hql).setParameter("id", id);
        return (Contact) query.uniqueResult();
    }

}
